package com.android.pinweilin.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

import com.android.pinweilin.activity.BaseObject;
import com.android.pinweilin.model.detail.paydonetoshare;
import com.android.pinweilin.model.detail.paydonetoshareCallback;
import com.android.pinweilin.utils.ConstantsUrl;
import com.android.pinweilin.utils.HttpUtils;
import com.android.pinweilin.utils.LoginState;
import com.android.pinweilin.utils.MyLog;
import com.android.pinweilin.utils.ObjectUtils;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.HashMap;

import okhttp3.Call;

/**
 * Created by Administrator on 2017/6/12.
 */

public  class  WXPayEntryActivity  extends Activity implements IWXAPIEventHandler{

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;
    private boolean networkConnected;
    private  WXPayEntryActivity mcontext;
    private SharedPreferences sharedPreferences;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.wechat_payresult);
         mcontext=WXPayEntryActivity.this;
        api = WXAPIFactory.createWXAPI(this, ConstantsUrl.PinTuan_WX_APPID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        finish();
//        setIntent(intent);
//        api.handleIntent(intent, this);
    }

  @Override
    public void onReq(BaseReq req) {

    }

    /**
     * 得到支付结果回调
     */
    @Override
    public void onResp(BaseResp resp) {
        switch (resp.errCode)
        {
            case 0:
                MyLog.e(TAG, "onPayFinish, errCode = " + resp.errCode);
                GetToSharePage();
                break;
            case -1:
                break;
            case -2:
                finish();
                break;
            default:
                break;
        }
      // 支付结果码
    }

    //支付成功去分享页面
    private void GetToSharePage()
    {
        networkConnected = ObjectUtils.isNetworkConnected(this);
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.MEMBERPAYDONETOShare;
            sharedPreferences = getSharedPreferences("wechat_temporary", 0);
           String order_sn= sharedPreferences.getString(ConstantsUrl.INTENT_KEY.MAIN_GROUP_TO_DETAIL_ORDER_SN,"");
            String getid = LoginState.getInstance().getid(mcontext);
            String getpd = LoginState.getInstance().getpd(mcontext);
            HashMap<String, String> header = new HashMap<>();
            header.put("UID", getid);
            header.put("UPSW",getpd);
            HashMap<String, String> map = new HashMap<>();
            map.put("order_sn", order_sn);
            MyLog.e("order_snweixin",order_sn.toString());
            HttpUtils.get(address, header,map, new paydonetoshareCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    MyLog.e("call",call.toString());
                    MyLog.e("error",e.getMessage());
                    MyLog.e("error1",e.toString());
                    finish();
                }

                @Override
                public void onResponse(paydonetoshare response, int id) {
                       MyLog.e("common_idweixin",response.getData().getCommon_id());
                    String common_id = response.getData().getCommon_id();
                    if(Integer.parseInt(common_id)>0) {
                        Intent intent_tuan = new Intent(mcontext, BaseObject.class);
                        intent_tuan.putExtra("title", "团详情");
                        intent_tuan.putExtra("need_id", common_id);
                        mcontext.startActivity(intent_tuan);
                    }
                    else
                    {

                    }
                    MyLog.e("common_id100","111111");
                    finish();
                }


            });

        } else {
            ObjectUtils.toast(mcontext, "网络未连接");

        }
    }
}
